﻿@{
    ViewBag.Title = "数据字典";
    Layout = "~/Views/Shared/_LayuiLayout.cshtml";
}
<blockquote class="layui-elem-quote">
    <div class="pos-r h-38" id="topbar">
        <span class="pos-a pos-left">
            <span class="layui-input-inline">
                <input type="text" id="tableName" placeholder="请输入表名称" autocomplete="off" class="layui-input" value="@ViewBag.searchTable">
            </span>
            <span class="layui-inline">
                <button type="button" class="layui-btn" data-type="Search_table"><i class="layui-icon">&#xe615;</i>搜索</button>
            </span>
        </span>
        <span class="pos-a pos-right">
            <span class="layui-input-inline">
                <input type="text" id="TableName" placeholder="请输入表名注释" autocomplete="off" class="layui-input layui-hide" hidden>
            </span>
            <span class="layui-input-inline">
                <button type="button" class="layui-btn layui-hide" data-type="Sure" hidden id="Sure">确定</button>
            </span>
            <span class="layui-input-inline">
                <input type="text" id="rowName" placeholder="请输入字段名称" autocomplete="off" class="layui-input">
            </span>
            <span class="layui-inline">
                <button type="button" class="layui-btn" data-type="Search_row"><i class="layui-icon">&#xe615;</i>搜索</button>
                <button type="button" class="layui-btn" data-type="Reload"><i class="layui-icon">&#xe669;</i>刷新</button>
                <button type="button" class="layui-btn" data-type="Describe">显示未描述字段</button>
                <button type="button" class="layui-btn" data-type="Export"><i class="layui-icon">&#xe67c;</i>导出所有表的字段</button>
            </span>
        </span>
    </div>
</blockquote>
<div class="layui-layout layui-layout-admin">
    <div class="layui-side" style="top: 70px;width:400px;">
        <div class="layui-side-scroll" style="width:400px;">
            <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
            <ul class="layui-nav layui-nav-tree site-demo-nav" style="margin-top:10px;width:400px;overflow:hidden">
                <li class="layui-nav-item layui-nav-itemed">
                    <a href="javascript:;">
                        数据字典查看
                        <span class="layui-nav-more"></span>
                    </a>
                    <dl class="layui-nav-child">
                        @Html.Raw(ViewBag.Menu)
                    </dl>
                </li>
            </ul>
        </div>
    </div>
    <div class="layui-body" style="top: 70px;overflow:hidden">
        <div style="padding-left: 215px;overflow:hidden">
            <table id="tbList" lay-filter="tbList"></table>
        </div>
    </div>
</div>
@*<div class="sogoutip" style="z-index: 2147483645; visibility: hidden; display: none;"></div>*@
@section scripts
{
    <script>
        var layer;//这两个变量设置为当前页的全局变量
        var showTable = "";
        function refresh(tb) {
            layui.use(['table', 'form', 'laydate', 'element', 'laytpl'], function () {
                var table = layui.table;
                var form = layui.form, laydate = layui.laydate, element = layui.element
                , laytpl = layui.laytpl;
                var $ = layui.jquery;
                layer = layui.layer;
                showTable = tb;

                table.render({
                    elem: '#tbList',
                    url: '@Url.Action("ListTB", "Dictionary")' + "?db=" + '@ViewBag.db' + "&table=" + tb,
                    method: 'post',
                    toolbar: true,
                    defaultToolbar: ['filter'],
                    //limit:90,
                    page: false,
                    height: 'full-128',
                    cols: [[
                        //{ type: 'checkbox', width: 50, fixed: 'left' },
                        { field: 'Id', title: "字段数", fixed: 'left' },
                        { field: 'TableName', title: "表名称", sort: true },
                        { field: 'Name', title: "字段名", sort: true },
                        { field: 'Description', title: "字段描述", sort: true,edit: 'text' },
                        { field: 'Type', sort: true, title: "类型" },
                        { field: 'IsKeyDesc', title: "主键", sort: true },
                        //{ field: 'IsIdentityDesc', title: "自增长", sort: true },
                        //{ field: 'IdentIncr', sort: true, title: "递增量"},
                        //{ field: 'Length', title: "长度", sort: true },
                        //{ field: 'Scale', sort: true, title: "小数" },
                        //{ field: 'IsNullableDesc', sort: true, title: "可空" },
                        //{ field: 'IdentSeed', sort: true, title: "标示种子" },
                        //{ field: 'Bytes', title: "占用字节数", sort: true, fixed: 'right' },
                    ]],
                });
                $(function () {
                    $("#TableName").removeClass("layui-hide");
                    $("#Sure").removeClass("layui-hide");
                    $.post("@Url.Action("TableName", "Dictionary")", { db: '@ViewBag.db', table: tb},function (res) {
                        $("#TableName").val(res.msg);
                    });
                });

            });

        };
    </script>
    <!-- 右侧表格设置-->
    <script>
        layui.use(['table', 'form', 'laydate', 'element', 'laytpl'], function () {
            var table = layui.table;
            var form = layui.form, laydate = layui.laydate, element = layui.element
            ,laytpl = layui.laytpl;
            var $ = layui.jquery;
            layer = layui.layer;

            $("#Sure").click(function () {
                var value=$("#TableName").val();
                if(value==""||value==null){
                    layer.msg("表名注释不能为空");
                    return;
                }
                $.post("@Url.Action("EditTableName", "Dictionary")", { db: '@ViewBag.db', table: showTable, value: value },
                    function (res) {
                        layer.msg("设置成功");
                        $("#" + showTable).text("("+value+")");
                });
             });
            //刷新(添加修改等子页面也需要用到)
            reload = function () {
                table.reload('tbList', {
                    where: { rowName: "" }
                });
                $('#rowName').val("");
            }

            //设置标题区块的按钮的事件方法
            var active = {
                Reload: function () {//刷新按钮
                    reload();
                },
                Describe: function () {
                    var tag =0;
                    if (showTable == "") {
                        tag=1
                    }
                    layer_show('', '显示未描述字段', '@Url.Action("Describe", "Dictionary")' + "?table=" + showTable + "&db=" + '@ViewBag.db' + "&tag=" + tag, '100%', '100%');
                },
                Search_table: function () {
                    window.location = '@Url.Action("Index", "Dictionary")' + "?searchTable=" + $("#tableName").val()
                },
                Search_row: function () {
                    table.reload('tbList',
                    {
                        where: { rowName: $('#rowName').val() }
                    });
                },
                Export: function () {
                    var loading = 0;
                    loading = layer.load();
                    $.post("@Url.Action("ExportNew", "Dictionary")", { db: '@ViewBag.db'}, function (res) {
                            layer.msg("导出成功");
                            layer.close(loading);
                            window.location.href = "/bgFile/File/excel/" + res.data;
                            $.post("@Url.Action("DeleteFile", "Dictionary")",{address:res.msg},function(res){

                            });
                        });
                },
            };

            //监听表头按钮点击
            $('#topbar .layui-btn').on('click', function () {
                //当按钮被单击时
                var type = $(this).data('type');//获取该按钮的type数据
                active[type] ? active[type].call(this) : '';//执行active中匹配type的方法
            });

            //监听排序
            table.on('sort(tbList)', function (obj) {
                table.reload('tbList', {
                    initSort: obj,
                    where: {
                        field: obj.field,
                        order: obj.type
                    }
                });
            });

            //监听单元格编辑
            table.on('edit(tbList)', function (obj) {
                var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field; //得到字段
                var loadIndex = layer.load();
                $.post("@Url.Action("CellEdit", "Dictionary")", {
                    db: '@ViewBag.db', table: showTable, colName: data.Name, value: value, colId: data.Id
                },
                    function (res) {
                        layer.close(loadIndex);
                });
            });
        });
    </script>
}
